//
//  ZGLSuccessCallBackVC.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/8/10.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLSuccessCallBackVC.h"
#import "ZGLRecordDetailController.h"
#import "ZGLLotteryRecordController.h"

@interface ZGLSuccessCallBackVC ()<UIGestureRecognizerDelegate>

@end

@implementation ZGLSuccessCallBackVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = ViewBackgroundColor;
    
    //隐藏返回键
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.hidesBackButton = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer
                                      *)gestureRecognizer{

    return NO; //YES：允许右滑返回  NO：禁止右滑返回
}


- (void)setUrl:(NSString *)url
{
    _url = url;
}

#pragma mark - meoth

//订单详情
- (void)checkOrderDetail:(UIButton *)sender
{
    DLog(@"%@",sender.titleLabel.text);
    [TalkingData trackEvent:ZGLBusinessPageOrderDetailBtnEventID];
    
    ZGLRecordDetailController *detailVc = [[ZGLRecordDetailController alloc]init];
    detailVc.urlString = self.url;
    
//    [self cw_pushViewController:detailVc drewerHiddenDuration:0.01];
    [self.navigationController pushViewController:detailVc animated:YES];
}

//继续购买
- (void)buyAgainClick:(UIButton *)sender
{
    DLog(@"%@",sender.titleLabel.text);
    NSString *btnString = sender.titleLabel.text;
    if ([btnString isEqualToString:@"继续购买"]) {
        [TalkingData trackEvent:ZGLBusinessPageBuyAgainBtnEventID];
        
    }else if ([btnString isEqualToString:@"返回重新选号"]){
        [TalkingData trackEvent:ZGLBusinessPageSelectAgainBtnEventID];
        
    }else if ([btnString isEqualToString:@"马上去投注"]){
        [TalkingData trackEvent:ZGLBusinessPageBuyNowBtnEventID];
        
    }else if ([btnString isEqualToString:@"完成"]){
        [TalkingData trackEvent:ZGLBusinessPageDoneBtnEventID];
    }
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [self UpdatePersonalInformation];
    });
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//查看购彩记录
- (void)checkOrderList:(UIButton *)sender
{
    DLog(@"%@",sender.titleLabel.text);
    [TalkingData trackEvent:ZGLBusinessPageCheckRecordListBtnEventID];
    
    ZGLLotteryRecordController *recordListVc = [[ZGLLotteryRecordController alloc]init];
    [self cw_pushViewController:recordListVc drewerHiddenDuration:0.01];
    
}

//更新个人信息
- (void)UpdatePersonalInformation
{
    if (sZGLUserInfoModel.usercheck) {
        
        [sZGLHTTPRequestManager UpdatePersonalInformationWithSuccess:^(id responsedata) {
            
            ZGLUserInfoModel *model = [ZGLUserInfoModel mj_objectWithKeyValues:responsedata];
            [sZGLUserInfoModel setLoginSuccessedWithUserInfo:model];
            DLog(@"更新后--%@",sZGLUserInfoModel.balance);
            
        } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {
            
            DLog("更新失败");
            
        }];
    }
}

#pragma mark - init

- (void)initWithCallBackStatus:(ZGLBusinessCallBack )callBack Status:(ZGLStatusCallBack )status
{
    switch (callBack) {
        case ZGLBusinessCallBackPay:
        {
            [self.view addSubview:self.statusImage];
            [self.view addSubview:self.statusMessageLabel];
            [self.view addSubview:self.supplyMessageLabel];
            [self.view addSubview:self.businessBgView];
            [self.view addSubview:self.subMessageLabel];
            [self.view addSubview:self.checkDetailButton];
            [self.view addSubview:self.doneButton];
            
            
            [_statusMessageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(self.view).offset(30);
                make.centerX.equalTo(self.view).offset(12);
                make.height.equalTo(@(24));
            }];
            
            [_statusImage mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.right.equalTo(_statusMessageLabel.mas_left).offset(-5);
                make.top.equalTo(_statusMessageLabel);
                make.size.equalTo(@(24));
                
            }];
            
            [_supplyMessageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(_statusMessageLabel.mas_bottom).offset(20);
                make.centerX.equalTo(self.view);
                make.height.equalTo(_statusMessageLabel);
            }];
            
            [_businessBgView mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(_supplyMessageLabel.mas_bottom).offset(30);
                make.left.equalTo(self.view);
                make.right.equalTo(self.view);
                make.height.equalTo(@(1));
                
            }];
            
            UIImageView *lineImage = [[UIImageView alloc]initWithFrame:CGRectMake(25, 1, UI_SCREEN_WIDTH-30, 1)];
            lineImage.image = [HKUIUtils imageWithLineWithImageView:lineImage];
            //添加虚线
            [_businessBgView addSubview:lineImage];
            _businessBgView.backgroundColor = ViewBackgroundColor;
            
            [_subMessageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(_businessBgView.mas_bottom).offset(30);
                make.centerX.equalTo(_supplyMessageLabel);
                
            }];
            
            [_checkDetailButton mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(_subMessageLabel.mas_bottom).offset(50);
                make.left.equalTo(self.view).offset(20);
                make.right.equalTo(_doneButton.mas_left).offset(-10);
                make.height.equalTo(@(40));
                make.width.equalTo(_doneButton);
            }];
            
            
            [_doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(_checkDetailButton);
                make.right.equalTo(self.view).offset(-20);
                make.left.equalTo(_checkDetailButton.mas_right).offset(10);
                make.height.equalTo(_checkDetailButton);
                make.width.equalTo(_checkDetailButton);
            }];
            
            if (status == ZGLStatusSuccess) {
                //支付成功
                [self.statusImage setImage:[UIImage imageNamed:@"img_buy_success"]];
                self.title = @"支付成功";
                self.statusMessageLabel.text = @"恭喜你，支付成功！";
                self.supplyMessageLabel.text = @"订单正在送往投注站";
                self.subMessageLabel.text = @"可通过订单详情查看订单状态";
                [_checkDetailButton setTitle:@"订单详情" forState:UIControlStateNormal];
                [_checkDetailButton addTarget:self action:@selector(checkOrderDetail:) forControlEvents:UIControlEventTouchUpInside];
                
                [_doneButton setTitle:@"继续购买" forState:UIControlStateNormal];
                [_doneButton addTarget:self action:@selector(buyAgainClick:) forControlEvents:UIControlEventTouchUpInside];
                
            }else if (status == ZGLStatusFail){
                //支付失败
                [self.statusImage setImage:[UIImage imageNamed:@"img_buy_fail"]];
                self.statusMessageLabel.text = @"支付失败";
                self.title  = @"支付失败";
                
                [_supplyMessageLabel setHidden:YES];
                [_businessBgView setHidden:YES];
                [_subMessageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.equalTo(_statusMessageLabel.mas_bottom).offset(30);
                    
                }];
                
                [_checkDetailButton setTitle:@"查看购彩记录" forState:UIControlStateNormal];
                [_checkDetailButton addTarget:self action:@selector(checkOrderList:) forControlEvents:UIControlEventTouchUpInside];
                
                [_doneButton setTitle:@"返回重新选号" forState:UIControlStateNormal];
                [_doneButton addTarget:self action:@selector(buyAgainClick:) forControlEvents:UIControlEventTouchUpInside];
                
            }
            
            
        }
            break;
        case ZGLBusinessCallBackRecharge:
        {
            [self.view addSubview:self.statusImage];
            [self.view addSubview:self.statusMessageLabel];
            [self.view addSubview:self.businessBgView];
            [_businessBgView addSubview:self.businessModeLabel];
            [_businessBgView addSubview:self.businessDetailLablel];
            [_businessBgView addSubview:self.AmountLabel];
            [_businessBgView addSubview:self.Amount];
            [self.view addSubview:self.doneButton];
            
            [_statusMessageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(self.view).offset(50);
                make.centerX.equalTo(self.view).offset(12);
                make.height.equalTo(@(24));
            }];
            
            [_statusImage mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.right.equalTo(_statusMessageLabel.mas_left).offset(-5);
                make.top.equalTo(_statusMessageLabel);
                make.size.equalTo(@(24));
                
            }];
            
            [_businessBgView mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(_statusMessageLabel.mas_bottom).offset(50);
                make.left.equalTo(self.view);
                make.right.equalTo(self.view);
                
            }];
            [HKUIUtils addLineInView:_businessBgView top:YES leftMargin:0 rightMargin:0];
            [HKUIUtils addLineInView:_businessBgView top:NO leftMargin:0 rightMargin:0];
            
            [_businessModeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.left.equalTo(_businessBgView).offset(30);
                make.top.equalTo(_businessBgView).offset(20);
                
            }];
            _businessModeLabel.text = @"充值方式";
            
            [_businessDetailLablel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(_businessModeLabel);
                make.right.equalTo(_businessBgView).offset(-20);
            }];
            
            [_AmountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(_businessModeLabel.mas_bottom).offset(10);
                make.left.equalTo(_businessModeLabel);
            }];
            _AmountLabel.text = @"金额";
            
            [_Amount mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(_AmountLabel);
                make.right.equalTo(_businessDetailLablel);
            }];
            
            [_businessBgView mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.bottom.equalTo(_Amount.mas_bottom).offset(20);
            }];
            
            [_doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(_businessBgView.mas_bottom).offset(40);
                make.left.equalTo(self.view).offset(20);
                make.right.equalTo(self.view).offset(-20);
                make.height.equalTo(@(40));
            }];
            
            [_doneButton setTitle:@"马上去投注" forState:UIControlStateNormal];
            [_doneButton addTarget:self action:@selector(buyAgainClick:) forControlEvents:UIControlEventTouchUpInside];
            
            self.statusMessageLabel.text = @"恭喜你，充值成功！";
            self.title = @"充值成功";
            
            
            
        }
            break;
        case ZGLBusinessCallBackExtractCash:
        {
            
            [self.view addSubview:self.businessBgView];
            [self.view addSubview:self.statusMessageLabel];
            [self.view addSubview:self.subMessageLabel];
            [_businessBgView addSubview:self.businessModeLabel];
            [_businessBgView addSubview:self.businessDetailLablel];
            [_businessBgView addSubview:self.AmountLabel];
            [_businessBgView addSubview:self.Amount];
            [self.view addSubview:self.doneButton];
            
            [_statusImage setHidden:YES];
            [_statusMessageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.equalTo(self.view).offset(30);
                make.centerX.equalTo(self.view);
            }];
            
            [_subMessageLabel mas_makeConstraints:^(MASConstraintMaker *make) {

                make.top.equalTo(_statusMessageLabel.mas_bottom).offset(30);
                make.centerX.equalTo(_statusMessageLabel);
            }];

            [_businessBgView mas_makeConstraints:^(MASConstraintMaker *make) {

                make.top.equalTo(_subMessageLabel.mas_bottom).offset(50);
                make.left.equalTo(self.view);
                make.right.equalTo(self.view);

            }];
            [HKUIUtils addLineInView:_businessBgView top:YES leftMargin:0 rightMargin:0];
            [HKUIUtils addLineInView:_businessBgView top:NO leftMargin:0 rightMargin:0];
            

            [_businessModeLabel mas_makeConstraints:^(MASConstraintMaker *make) {

                make.left.equalTo(_businessBgView).offset(30);
                make.top.equalTo(_businessBgView).offset(20);

            }];
            _businessModeLabel.text = @"银行卡";

            [_businessDetailLablel mas_makeConstraints:^(MASConstraintMaker *make) {

                make.top.equalTo(_businessModeLabel);
                make.right.equalTo(_businessBgView).offset(-20);
            }];

            [_AmountLabel mas_makeConstraints:^(MASConstraintMaker *make) {

                make.top.equalTo(_businessModeLabel.mas_bottom).offset(10);
                make.left.equalTo(_businessModeLabel);
            }];
            _AmountLabel.text = @"金额";

            [_Amount mas_makeConstraints:^(MASConstraintMaker *make) {

                make.top.equalTo(_AmountLabel);
                make.right.equalTo(_businessDetailLablel);
            }];

            [_businessBgView mas_makeConstraints:^(MASConstraintMaker *make) {

                make.bottom.equalTo(_Amount.mas_bottom).offset(20);
            }];

            [_doneButton mas_makeConstraints:^(MASConstraintMaker *make) {

                make.top.equalTo(_businessBgView.mas_bottom).offset(40);
                make.left.equalTo(self.view).offset(20);
                make.right.equalTo(self.view).offset(-20);
                make.height.equalTo(@(40));
            }];

            [_doneButton setTitle:@"完成" forState:UIControlStateNormal];
            [_doneButton addTarget:self action:@selector(buyAgainClick:) forControlEvents:UIControlEventTouchUpInside];
            
            
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - getter

- (UIImageView *)statusImage
{
    if (!_statusImage) {
        _statusImage = [[UIImageView alloc]init];
        _statusImage.image = [UIImage imageNamed:@"img_buy_success"];
    }
    return _statusImage;
}

- (UILabel *)statusMessageLabel
{
    if (!_statusMessageLabel) {
        _statusMessageLabel = [[UILabel alloc]init];
        _statusMessageLabel.font = [UIFont systemFontOfSize:23];
        _statusMessageLabel.numberOfLines = 0;
    }
    return _statusMessageLabel;
}

- (UILabel *)supplyMessageLabel
{
    if (!_supplyMessageLabel) {
        _supplyMessageLabel = [[UILabel alloc]init];
        _supplyMessageLabel.font = [UIFont systemFontOfSize:20];
        _supplyMessageLabel.numberOfLines = 0;
    }
    return _supplyMessageLabel;
    
}

- (UILabel *)subMessageLabel
{
    if (!_subMessageLabel) {
        _subMessageLabel = [[UILabel alloc]init];
        _subMessageLabel.font = [UIFont systemFontOfSize:15];
        _subMessageLabel.textColor = [UIColor lightGrayColor];
    }
    return _subMessageLabel;
}

- (UIView *)businessBgView
{
    if (!_businessBgView) {
        _businessBgView = [[UIView alloc]init];
        _businessBgView.backgroundColor = [UIColor whiteColor];
        
        
    }
    return _businessBgView;
}

- (UILabel *)businessModeLabel
{
    if (!_businessModeLabel) {
        _businessModeLabel = [[UILabel alloc]init];
        _businessModeLabel.textColor = [UIColor lightGrayColor];
        _businessModeLabel.font = [UIFont systemFontOfSize:15];
        _businessModeLabel.textAlignment = 0;
        
    }
    return _businessModeLabel;
}

- (UILabel *)businessDetailLablel
{
    if (!_businessDetailLablel) {
        _businessDetailLablel = [[UILabel alloc]init];
        _businessDetailLablel.font = [UIFont systemFontOfSize:15];
        _businessDetailLablel.textAlignment = 2;
        
    }
    
    return _businessDetailLablel;
}

- (UILabel *)AmountLabel
{
    if (!_AmountLabel) {
        _AmountLabel = [[UILabel alloc]init];
        _AmountLabel.textColor = [UIColor lightGrayColor];
        _AmountLabel.font = [UIFont systemFontOfSize:15];
        _AmountLabel.textAlignment = 0;
    }
    return _AmountLabel;
}

- (UILabel *)Amount
{
    if (!_Amount) {
        _Amount = [[UILabel alloc]init];
        _Amount.font = [UIFont systemFontOfSize:15];
        _Amount.textAlignment = 2;
        
    }
    return _Amount;
}

- (UIButton *)checkDetailButton
{
    if (!_checkDetailButton) {
        _checkDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _checkDetailButton.layer.backgroundColor = [UIColor whiteColor].CGColor;
        _checkDetailButton.layer.cornerRadius = 3;
        _checkDetailButton.layer.masksToBounds = YES;
        _checkDetailButton.layer.borderColor = BorderColor.CGColor;
        _checkDetailButton.layer.borderWidth = kBorderWidth;
        _checkDetailButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_checkDetailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    return _checkDetailButton;
}

- (UIButton *)doneButton
{
    if (!_doneButton) {
        _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _doneButton.layer.backgroundColor = globalRed.CGColor;
        _doneButton.layer.cornerRadius = 3;
        _doneButton.layer.masksToBounds = YES;
        _doneButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    
    return _doneButton;
}


@end
